﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Client.UIL.ManagePrinter;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class MemberValueRecordEdit : UserControl
    {
        public MemberValueRecordLogic LgcMemberValueRecord;
        public T_MemberValueRecord TmemberValueRecord;
        public T_Member Tmember;
        public MemberLogic LgcMember;
        public AdminLogic LgcAdmin;
        public List<V_Admin> Admins; 
        public List<T_Borakerage> Borakerages;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public MemberValueRecordEdit()
        {
            InitializeComponent();
            LgcMemberValueRecord = new MemberValueRecordLogic();
            LgcMember=new MemberLogic();
            Tmember = new T_Member();
            TmemberValueRecord = new T_MemberValueRecord();
            LgcAdmin = new AdminLogic();
            Admins=new List<V_Admin>();
            Borakerages = new List<T_Borakerage>();
            var moenyLogic = new MoneyLogic();
            var moenyList = moenyLogic.SqlWhere<T_Account>(" 1=1").ToList();
            var list = moenyList.Where(m => m.Name != "现金").ToList();
            cmbBankAccountId.DataSource = list;
            cmbBankAccountId.DisplayMember = "Name";
            cmbBankAccountId.ValueMember = "Id";

            //var admins = LgcAdmin.GetAdminViewFast();
            //cmbAdmin.DataSource = admins;
            //cmbAdmin.DisplayMember = "Name";
            //cmbAdmin.ValueMember = "Id";
            //cmbAdmin.Text = S_Admin.Name;
            Admins = LgcAdmin.GetAdminView().ToList();
            dgvCmbAdmin.DataSource = Admins;
            dgvCmbAdmin.DisplayMember = "Name";
            dgvCmbAdmin.ValueMember = "Id";
            if (Admins.Any())
            {
                dgvCmbAdmin.DefaultCellStyle.NullValue = Admins[0].Name;
                var borakerage = new T_Borakerage { AdminId = Admins[0].Id };
                Borakerages.Add(borakerage);
            }
            dgvBorakerage.DataSource = Borakerages;
        }

        private void MemberValueRecordEdit_Load(object sender, EventArgs e)
        {
            nudCashPay.Focus();
        }

        public void GetMember(T_Member tmember)
        {
            Tmember = tmember;
            var vMember = LgcMember.GetMemberViewObject(" where T_Member.Id ='" + Tmember.Id + "'");
            //txtTreeId.Text =Tmember.t
            lblTreeName.Text = vMember.TreeName;
            txtMemberId.Text = tmember.CardId + " " + tmember.Name;
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }


        private void BtnSave_Click(object sender, EventArgs e)
        {
            TmemberValueRecord.MemberId = Tmember.Id;
            TmemberValueRecord.CashPay = nudCashPay.Value;
            TmemberValueRecord.BankPay = nudBankPay.Value;
            TmemberValueRecord.GiveValue = nudGive.Value;
            TmemberValueRecord.BankAccountId = cmbBankAccountId.SelectedValue.ToString();
            TmemberValueRecord.InValue = nudCashPay.Value + nudBankPay.Value + nudGive.Value;
            TmemberValueRecord.AddTime = dtpAddTime.Value;
            TmemberValueRecord.Remark = txtRemark.Text;
            TmemberValueRecord.AdminId = S_Admin.Id;
            TmemberValueRecord.ShopId = S_Admin.ShopId;

            dgvBorakerage.EndEdit(DataGridViewDataErrorContexts.Commit);
            int row = LgcMemberValueRecord.InValue(TmemberValueRecord, Tmember, Borakerages);
            string message = "";
            message = row == 0 ? "保存失败" : "保存成功";
            if (row == 0) FormSysMessage.ShowSuccessMsg(message);
            else MemberList.Instance.btnSearch_Click(null,null);

            ValueRecordPrintWindow.Instance.SetReport(TmemberValueRecord.Id);

            //print.SetReport("OpenSoftStore.Client.UIL.ManagePrinter.BillPrint.rdlc", "DataSet1", data);
            ValueRecordPrintWindow.Instance.ShowDialog();

            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void btnGetTree_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("MemberTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            Tmember.TreeId = formTreeSelecter.TypeId;
            txtTreeId.Text = formTreeSelecter.TypeText;
            var memberTree = LgcMember.Where<T_MemberTree>(m => m.TreeId == Tmember.TreeId).FirstOrDefault();
            if (memberTree != null) nudGive.Value = memberTree.Present;
            if (memberTree == null) nudGive.Value = 0;
            SetInValue();

        }
        public void SetInValue()
        {
            txtInValue.Text = (nudCashPay.Value + nudBankPay.Value + nudGive.Value).ToString();
        }
        private void nudCashPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetInValue();
        }

        private void nudBankPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetInValue();
        }
        private void nudGive_KeyUp(object sender, KeyEventArgs e)
        {
            SetInValue();
        }

        private void btnBorakerageAdd_Click(object sender, EventArgs e)
        {
            dgvBorakerage.DataSource = null;
            //dgvBorakerage.EndEdit(DataGridViewDataErrorContexts.Commit);
            var borakerage = new T_Borakerage { AdminId = Admins[0].Id };
            Borakerages.Add(borakerage);
            dgvBorakerage.DataSource = Borakerages;
            dgvBorakerage.Focus();
        }

        private void btnBorakerageDelete_Click(object sender, EventArgs e)
        {
            if (dgvBorakerage.CurrentRow == null) return;
            var index = dgvBorakerage.CurrentRow.Index;
            Borakerages.RemoveAt(index);
            dgvBorakerage.DataSource = null;
            dgvBorakerage.DataSource = Borakerages;
            if (index > 0) dgvBorakerage.CurrentCell = dgvBorakerage.Rows[index - 1].Cells[0];
            dgvBorakerage.Focus();
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
 

    }
}
